package cn.tedu.yushougeproduct.service.impl;

import cn.tedu.yushouge.commons.ex.ServiceException;
import cn.tedu.yushouge.commons.web.ServiceCode;
import cn.tedu.yushougeproduct.mapper.AttributeMapper;
import cn.tedu.yushougeproduct.mapper.AttributeTemplateMapper;
import cn.tedu.yushougeproduct.pojo.entity.Attribute;
import cn.tedu.yushougeproduct.pojo.param.AttributeAddNewParam;
import cn.tedu.yushougeproduct.pojo.vo.AttributeStandardVO;
import cn.tedu.yushougeproduct.service.IAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AttributeServiceImpl implements IAttributeService {

    @Autowired
    AttributeMapper attributeMapper;

    @Autowired
    AttributeTemplateMapper attributeTemplateMapper;

    @Override
    public void addNew(AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【添加属性】的业务，参数：{}", attributeAddNewParam);

        String name = attributeAddNewParam.getName();
        Long templateId = attributeAddNewParam.getTemplateId();
        int countByName = countByNameAndTemplate(name, templateId);
        log.debug("尝试在属性模板【{}】中添加属性【{}】，在数据库中此名称的属性数量为：{}", templateId, name, countByName);

        if (countByName > 0){
            String message = "添加属性失败！此属性模板中已经存在名称为【" + name + "】的属性！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        {
            Object queryResult = attributeTemplateMapper.getStandardById(templateId);
            if (queryResult == null){
                String message = "添加属性失败！属性模板不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewParam,attribute);
        attribute.setTemplate_id(templateId);
        attribute.setGmtCreate(LocalDateTime.now());
        log.debug("即将向数据库中插入数据：{}", attribute);
        int rows = attributeMapper.insert(attribute);
        if (rows != 1) {
            String message = "添加属性失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    private int countByNameAndTemplate(String name,Long  templateId){
        log.debug("开始执行【统计匹配名称且匹配属性模板的属性的数量】的数据访问，名称：{}，属性模板：{}", name, templateId);
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("template_id",templateId);
        return attributeMapper.selectCount(queryWrapper);
    }

    @Override
        public List<AttributeStandardVO> listByTemplateId(Long templateId) {
        log.debug("开始处理【根据属性模板查询属性列表】的业务，属性模板：{}", templateId);
        return attributeMapper.listByTemplateId(templateId);
    }
}
